package com.gushenge.core.impls

import android.util.Log
import com.gushenge.atools.util.ADate
import com.gushenge.core.*
import com.gushenge.core.beans.*
import com.gushenge.core.beans.Collection
import com.gushenge.core.dao.GlobalConsts
import com.gushenge.core.dao.GlobalKeys.KEY
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.dao.MMKVConsts.sub
import com.gushenge.core.dao.MMKVConsts.uid
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.models.UserModel
import com.rxlife.coroutine.RxLifeScope
import org.jetbrains.anko.toast
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import rxhttp.toClass
import rxhttp.wrapper.param.RxHttp

object UserImpl:UserModel {


    override fun loginByQqWx(openid: String, type: String,password:String? ,invite:String? ,listener: Code<String>.() -> Unit) {


        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.LOGIN_BY_QQ_WX)
                .add("openid", openid)
                .add("type", type)
                .add("language", MMKVConsts.language)
                .add("passwd", password)
                .add("invite", invite)
                .add("member_id", sub)
                .add("member_id", sub)
                .toClass<Code<String>>()
                .await()
            listener(result)
        }



//        HttpQuery().loginByQQWx(GlobalConsts.LOGIN_BY_QQ_WX, openid, type,sub,MMKVConsts.sub,).enqueue(object : Callback<Code<String>?> {
//            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                CoreApplication.instance.toast(t.toString())
//            }
//
//            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
//                response.body() ?: run {
//                    CoreApplication.instance.toast("登录失败")
//                }
//                response.body()?.let {
//
//                }
//            }
//        })
    }

    override fun emailCode(email: String, type: Int, listener: ResultListener) {
        HttpQuery().emaiLCode(GlobalConsts.EMAIL_CODE, email, type,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body() ?: listener.error("获取失败")
                response.body()?.apply {
                    if (code == 1) {
                        listener.success(data ?: "")
                    } else {
                        listener.error(message)
                    }
                }
            }
        })
    }

    override fun unBindPhone(phone: String, code: String, sessionid: String, listener: ResultListener) {

        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().unbindPhone(GlobalConsts.UNBIND_PHONE, uid, time, sign, phone, code, sessionid,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body() ?: listener.error("解绑失败")
                response.body()?.apply {
                    if (this.code == 1) {
                        listener.success()
                        listener.error(message)
                    } else {
                        listener.error(message)
                    }
                }
            }
        })
    }

    override fun unBindEmail(email: String, code: String, sessionid: String, listener: ResultListener) {

        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().unbindEmail(GlobalConsts.UNBIND_EMAIL, uid, time, sign, email, code, sessionid,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body() ?: listener.error("解绑失败")
                response.body()?.apply {
                    if (this.code == 1) {
                        listener.success()
                        listener.error(message)
                    } else {
                        listener.error(message)
                    }
                }
            }
        })
    }

    override fun retrievePassword(phone: String?, email: String?, code: String, sessionid: String, password: String, listener: ResultListener) {
        HttpQuery().retrievePassword(GlobalConsts.RETRIEVE_PASSWORD, phone, email, code, sessionid, password,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body() ?: listener.error("重置失败")
                response.body()?.apply {
                    if (this.code == 1) {
                        listener.success()
                        listener.error(message)
                    } else {
                        listener.error(message)
                    }
                }
            }
        })
    }

    override fun bill(type: Int, p: Int, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().pointDetail(GlobalConsts.BILL, uid, type, p, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<PointDetail>?> {
            override fun onFailure(call: Call<Codes<PointDetail>?>, t: Throwable) {
                listener.error()
            }

            override fun onResponse(call: Call<Codes<PointDetail>?>, response: Response<Codes<PointDetail>?>) {
                response.body() ?: listener.error()
                response.body()?.let {
                    when (it.code) {
                        1 -> listener.success(it.data, it.p, it.max_p)
                        else -> listener.error()
                    }
                }
            }
        })
    }

    override fun tiedMoney(listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$uid${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().tiedMoney(GlobalConsts.TIEDMONEY, uid, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<TiedMoney>?> {
            override fun onFailure(call: Call<Codes<TiedMoney>?>, t: Throwable) {
                Log.e("small", t.toString())
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Codes<TiedMoney>?>, response: Response<Codes<TiedMoney>?>) {
                response.body() ?: listener.error()
                response.body()?.let {
                    if (it.code == 1) listener.success(it.data) else listener.error(it.message)
                }
            }
        })
    }



    override fun small(listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$uid${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().small(GlobalConsts.SMALL, uid, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<Small>?> {
            override fun onFailure(call: Call<Codes<Small>?>, t: Throwable) {
                Log.e("small", t.toString())
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Codes<Small>?>, response: Response<Codes<Small>?>) {
                response.body() ?: listener.error()
                response.body()?.let {
                    if (it.code == 1) listener.success(it.data) else listener.error(it.message)
                }
            }
        })
    }

    override fun myCollect(type: Int, p: Int, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$uid${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().myCollect(GlobalConsts.MYCOLLECT, uid, type, p, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<Collection>?> {
            override fun onFailure(call: Call<Codes<Collection>?>, t: Throwable) {
                listener.error()
            }

            override fun onResponse(call: Call<Codes<Collection>?>, response: Response<Codes<Collection>?>) {
                response.body()?:listener.error()
                response.body()?.let {
                    if (it.code==1) listener.success(it.data,it.p,it.max_p) else listener.error()
                }
            }
        })

    }


    override fun collectCancel(id: String, type:Int,listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$uid${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().collectCancel(GlobalConsts.GAME_COLLECT_CANCEL, uid, id, type, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body()?:listener.error("取消失败")
                response.body()?.let {
                    when(it.code){
                        1->listener.success()
                        else->listener.error(it.message)
                    }
                }
            }
        })
    }
    override fun collect(id: String,type:Int, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$uid${time}fsd213ewdsadqwe2121213edsad")
        Log.e("rerer", "$id.........$type")
        HttpQuery().collect(GlobalConsts.COLLECT, uid, id, type, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body()?:listener.error("收藏失败")
                response.body()?.let {
                    when(it.code){
                        1 -> {
                            listener.success(it.data.toString())
//                            Log.e("!23123", it.data.toString())
                        }
                        else->listener.error(it.message)
                    }
                }
            }
        })
    }

    override fun bindEmail(email: String, code: Int, sessionid: String, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$uid$time$KEY")
        HttpQuery().bindEmail(GlobalConsts.BIND_EMAIL, uid, email, sessionid, code, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body() ?: listener.error("绑定失败")
                response.body()?.let {
                    when (it.code) {
                        1 -> listener.success(it.data ?: "绑定成功")
                        else -> listener.error(it.message)
                    }
                }
            }
        })
    }

    override fun bindEmailCode(email: String, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$uid$time$KEY")
        HttpQuery().bindEmailCode(GlobalConsts.BIND_EMAIL_CODE, uid, email, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body() ?: listener.error("验证码获取失败")
                response.body()?.let {
                    when (it.code) {
                        1 -> listener.success(it.data ?: "")
                        else -> listener.error(it.message)
                    }
                }
            }
        })
    }

    override fun changePassword(oldPassword: String, password: String, rePassword: String, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$uid$time$KEY")
        HttpQuery().changePassword(GlobalConsts.CHANGE_PASSWORD, uid, oldPassword, password, rePassword, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body()?:listener.error("修改失败")
                response.body()?.let {
                    when(it.code){
                        1->listener.success(it.message)
                        else->listener.error(it.message)
                    }
                }
            }
        })
    }

    override fun bindPhoneCode(phone: String, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$uid$time$KEY")
        HttpQuery().bindPhoneCode(GlobalConsts.BIND_PHONE_CODE, uid, phone, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body()?:listener.error("验证码获取失败")
                response.body()?.let {
                    when(it.code){
                        1->listener.success(it.data?:"")
                        else->listener.error(it.message)
                    }
                }
            }
        })
    }

    override fun bindPhone(phone: String, code: Int, sessionid: String, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$uid$time$KEY")
        HttpQuery().bindPhone(GlobalConsts.BIND_PHONE, uid, phone, sessionid, code, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body()?:listener.error("绑定失败")
                response.body()?.let {
                    when(it.code){
                        1->listener.success(it.data?:"绑定成功")
                        else->listener.error(it.message)
                    }
                }
            }
        })
    }

    override fun login(name: String, password: String, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$name$password${time}riowreopfdwrops21qe")
        HttpQuery().login(GlobalConsts.LOGIN, name, password, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                listener.error(t.toString())
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body()?:listener.error("登录失败,请稍后重试")
                response.body()?.let {
                    when(it.code){
                        1->{
                            uid = it.data!!
                            listener.success()
                        }
                        else->listener.error(it.message)
                    }
                }
            }
        })
    }

    override fun deleteAddress(id: String, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$uid${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().deleteAddress(GlobalConsts.DELETE_ADDRESS, uid, time, sign, id,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<Address>?> {
            override fun onFailure(call: Call<Codes<Address>?>, t: Throwable) {
//                listener.error(t.message?:"删除失败")
            }

            override fun onResponse(call: Call<Codes<Address>?>, response: Response<Codes<Address>?>) {
                response.body()?.let {
                    when(it.code){
                        1->listener.success(it.message)
                        else->listener.error(it.message)
                    }
                }
                response.body()?:listener.error("删除失败")
            }
        })
    }


    /*新加地址*/
    override fun addAddress(value: String, listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("$uid${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().changeUserInfo(GlobalConsts.ADD_ADDRESS, uid, time, sign, value,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<UserInfo>?> {
            override fun onFailure(call: Call<Code<UserInfo>?>, t: Throwable) {
//                t.message?.let { listener.error(it) }
            }

            override fun onResponse(call: Call<Code<UserInfo>?>, response: Response<Code<UserInfo>?>) {
                response.body()?.let {
                    when(it.code){
                        1 -> listener.success(it.message)
                        else -> listener.error(it.message)
                    }
                }
                response.body()?:listener.error("新增地址失败")
            }
        })
    }

    /*修改用户信息*/
    override fun changeUserInfo(value:String,listener: ResultListener) {

        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().changeUserInfo(GlobalConsts.CHANGE_USER_INFO, uid, time, sign, aesEcb(value),MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<UserInfo>?> {
            override fun onFailure(call: Call<Code<UserInfo>?>, t: Throwable) {
//                t.message?.let { listener.error(it) }
            }

            override fun onResponse(call: Call<Code<UserInfo>?>, response: Response<Code<UserInfo>?>) {
                response.body()?.let {
                    when(it.code){
                        1->{
                            listener.success(it.message)
                        }
                        else->listener.error(it.message)
                    }
                }
            }
        })
    }

    /*获取个人信息*/
    override fun userInfo(listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().userInfo(GlobalConsts.USER_INFO, uid, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<UserInfo>?> {
            override fun onFailure(call: Call<Code<UserInfo>?>, t: Throwable) {
//
            }

            override fun onResponse(call: Call<Code<UserInfo>?>, response: Response<Code<UserInfo>?>) {
                response.body()?.let {
                    when(it.code){
                        1->{
                            it.data?.let { listener.success(it) }
                        }
                        else->listener.error(it.message)
                    }
                }
                response.body()?:listener.error("获取用户信息失败,请稍后重试")
            }
        })
    }

    /*地址查询*/
    override fun selectAddress(listener: ResultListener) {
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        HttpQuery().selectAddress(GlobalConsts.SELECT_ADDRESS, uid, time, sign,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Codes<Address>?> {
            override fun onFailure(call: Call<Codes<Address>?>, t: Throwable) {
//                t.message?.let {
//                    listener.error(it)
//                }
            }

            override fun onResponse(call: Call<Codes<Address>?>, response: Response<Codes<Address>?>) {
                response.body()?.let {
                    when(it.code){
                        1->{
                            it.data.let { listener.success(it) }
                        }
                        else->listener.error(it.message)
                    }
                }
                response.body()?:listener.error("查询失败,请稍后重试")
            }
        })
    }
    /*手机号注册获取验证码*/
    override fun getRegCode(phone: String, type: Int, listener: ResultListener) {
        HttpQuery().getRegCode(GlobalConsts.GETREGCODE, phone, type,MMKVConsts.sub,MMKVConsts.language).enqueue(object : Callback<Code<String>?> {
            override fun onFailure(call: Call<Code<String>?>, t: Throwable) {
//                t.message?.let { listener.error(it) }
            }

            override fun onResponse(call: Call<Code<String>?>, response: Response<Code<String>?>) {
                response.body()?.let {
                    when(it.code){
                        1 -> it.data?.let { listener.success(it) }
                        else-> it.message.let { listener.error(it) }
                    }
                }
                response.body()?:listener.error("获取失败,请稍后重试")
            }
        })
    }
    //设置密码  一键登录  第三方登录
    fun setPassword(token:String,password: String,invite:String,listener: Code<String>.() -> Unit){
        val time = ADate.getStampAs13()
        val sign = md5("${token}${time}fsd213ewdsadqwe2121213edsad")
        RxLifeScope().launch {
            val result = RxHttp.get(GlobalConsts.SET_PASSWORD)
                .add("uid", token)
                .add("t", time)
                .add("sign", sign)
                .add("passwd", password)
                .add("invite", invite)
                .add("member_id", sub)
                .toClass<Code<String>>()
                .await()
            listener(result)
        }
    }


    //
    fun checkReg(token:String,listener: Code<String>.() -> Unit){
        RxLifeScope().launch {
            val result = RxHttp.postForm(GlobalConsts.CheckREG)
                .add("str", token)
                .add("type", 2)
                .add("member_id", sub)
                .toClass<Code<String>>()
                .await()
            listener(result)
        }
    }
}